package com.zkthink.ceres.applet.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkthink.base.R;
import com.zkthink.ceres.applet.ceresStore.mapper.CeresStoreMapper;
import com.zkthink.ceres.applet.comment.mapper.AppletCommentMapper;
import com.zkthink.ceres.applet.common.domin.vo.PageDateTypeIdVo;
import com.zkthink.ceres.applet.pcVehicles.domain.entity.PcVehicles;
import com.zkthink.ceres.applet.pcVehicles.mapper.PcVehiclesMapper;
import com.zkthink.ceres.applet.product.domain.entity.AppletStoreProduct;
import com.zkthink.ceres.applet.product.domain.entity.AppletVehiclesProduct;
import com.zkthink.ceres.applet.product.domain.entity.CeresProductCategory;
import com.zkthink.ceres.applet.product.domain.vo.InsertPrivateProductOrServerVo;
import com.zkthink.ceres.applet.product.domain.vo.InsertPublicOrServerVo;
import com.zkthink.ceres.applet.product.domain.vo.MyServerOrProductVo;
import com.zkthink.ceres.applet.product.mapper.AppletStoreProductMapper;
import com.zkthink.ceres.applet.product.mapper.AppletVehiclesProductMapper;
import com.zkthink.ceres.applet.product.mapper.CeresProductCategoryMapper;
import com.zkthink.ceres.applet.server.domain.entity.AppletServer;
import com.zkthink.ceres.applet.server.domain.entity.AppletStoreServer;
import com.zkthink.ceres.applet.server.domain.entity.AppletVehiclesServer;
import com.zkthink.ceres.applet.server.domain.entity.PcServerCategory;
import com.zkthink.ceres.applet.server.domain.vo.FindServerDetailVo;
import com.zkthink.ceres.applet.server.mapper.AppletServerMapper;
import com.zkthink.ceres.applet.server.mapper.AppletStoreServerMapper;
import com.zkthink.ceres.applet.server.mapper.AppletVehiclesServerMapper;
import com.zkthink.ceres.applet.server.mapper.PcServerCategoryMapper;
import com.zkthink.ceres.member.dao.MemberMapper;
import com.zkthink.ceres.member.entity.Member;
import com.zkthink.ceres.store.dto.CommentDto;
import com.zkthink.ceres.store.dto.StoreProjectXianqing;
import com.zkthink.ceres.store.entity.Store;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zkthink.ceres.applet.product.domain.entity.AppletProduct;
import com.zkthink.ceres.applet.product.mapper.AppletProductMapper;
import com.zkthink.ceres.applet.product.service.AppletProductService;
import org.springframework.transaction.annotation.Transactional;

import static com.zkthink.ceres.applet.authority.config.AuthorityWebConfiguration.getMid;

/**
 * @Author cmz
 * @Date 2024-04-30 13:02
 */
@Service
public class AppletProductServiceImpl extends ServiceImpl<AppletProductMapper, AppletProduct> implements AppletProductService {

    @Autowired
    private AppletProductMapper appletProductMapper;

    @Autowired
    private AppletServerMapper appletServerMapper;

    @Autowired
    private AppletCommentMapper appletCommentMapper;

    @Autowired
    private CeresStoreMapper ceresStoreMapper;
    @Autowired
    private PcVehiclesMapper pcVehiclesMapper;

    @Autowired
    private AppletStoreProductMapper appletStoreProductMapper;

    @Autowired
    private AppletVehiclesProductMapper appletVehiclesProductMapper;

    @Autowired
    private AppletStoreServerMapper appletStoreServerMapper;

    @Autowired
    private AppletVehiclesServerMapper appletVehiclesServerMapper;

    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private PcServerCategoryMapper pcServerCategoryMapper;
    @Autowired
    private CeresProductCategoryMapper ceresProductCategoryMapper;


    @Override
    public R findByPage(PageDateTypeIdVo pageDateTypeIdVo) {
        QueryWrapper<AppletProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete","0").eq("status","1");
        if (pageDateTypeIdVo.getKeyword() != null) {
            queryWrapper.like("product_name", pageDateTypeIdVo.getKeyword());
        }
        if (pageDateTypeIdVo.getTypeId() != null) {
            queryWrapper.eq("category_id", pageDateTypeIdVo.getTypeId());
        }
        Page<AppletProduct> page = new Page<>(pageDateTypeIdVo.getPageNum(), pageDateTypeIdVo.getPageSize());
        Page<AppletProduct> appletProductPage = appletProductMapper.selectPage(page, queryWrapper);
        return R.success(appletProductPage);
    }

    @Override
    public R detail(FindServerDetailVo findServerDetailVo) {
        StoreProjectXianqing productDetailsVo = new StoreProjectXianqing();
        if (findServerDetailVo.getProductType().equals("产品分类")) {
            //商品信息
            QueryWrapper<AppletProduct> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", findServerDetailVo.getProductId());
            AppletProduct appletProduct = appletProductMapper.selectOne(queryWrapper);

            productDetailsVo.setId(appletProduct.getId());
            productDetailsVo.setTitle(appletProduct.getProductName());
            productDetailsVo.setSubtitle(appletProduct.getShortName());
            productDetailsVo.setImg(appletProduct.getProductImg());
            productDetailsVo.setBackgroundimg(appletProduct.getBackgroundImg());
            productDetailsVo.setPrice(appletProduct.getStorePrice());
            productDetailsVo.setContent(appletProduct.getContent());
            productDetailsVo.setRemarks(appletProduct.getRemarks());
            findServerDetailVo.setProductType("1");
            //店铺产品价格中检索
/*            QueryWrapper<AppletStoreProduct> appletStoreProductQueryWrapper = new QueryWrapper<>();
            appletStoreProductQueryWrapper.eq("product_id",findServerDetailVo.getProductId());
            appletStoreProductQueryWrapper.eq("group_buy_price",findServerDetailVo.getPrice());
            appletStoreProductQueryWrapper.orderByDesc("store_price");
            AppletStoreProduct appletStoreProduct = appletStoreProductMapper.selectOne(appletStoreProductQueryWrapper);
            if (appletStoreProduct==null){
                QueryWrapper<AppletVehiclesProduct> appletVehiclesProductQueryWrapper = new QueryWrapper<>();
                appletVehiclesProductQueryWrapper.eq("product_id",findServerDetailVo.getProductId());
                appletVehiclesProductQueryWrapper.eq("group_buy_price",findServerDetailVo.getPrice());
                appletVehiclesProductQueryWrapper.orderByDesc("store_price");
                AppletVehiclesProduct appletVehiclesProduct = appletVehiclesProductMapper.selectOne(appletVehiclesProductQueryWrapper);
                productDetailsVo.setStorePrice(appletVehiclesProduct.getStorePrice());
            }else {
                productDetailsVo.setStorePrice(appletStoreProduct.getStorePrice());
            }*/

        } else {
            //服务信息
            QueryWrapper<AppletServer> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", findServerDetailVo.getProductId());
            AppletServer appletServer = appletServerMapper.selectOne(queryWrapper);

            //设置服务基本信息
            productDetailsVo.setId(appletServer.getId());
            productDetailsVo.setTitle(appletServer.getTitle());
            productDetailsVo.setSubtitle(appletServer.getSubtitle());
            productDetailsVo.setImg(appletServer.getImg());
            productDetailsVo.setPrice(appletServer.getStorePrice());
            productDetailsVo.setContent(appletServer.getContent());
            productDetailsVo.setRemarks(appletServer.getRemarks());
            productDetailsVo.setBackgroundimg(appletServer.getBackgroundImg());

            //价格信息
            //店铺服务价格中检索
/*            QueryWrapper<AppletStoreServer> appletStoreProductQueryWrapper = new QueryWrapper<>();
            appletStoreProductQueryWrapper.eq("server_id",findServerDetailVo.getProductId());
            appletStoreProductQueryWrapper.eq("group_buy_price",findServerDetailVo.getPrice());
            appletStoreProductQueryWrapper.orderByDesc("store_price");
            AppletStoreServer appletStoreServer = appletStoreServerMapper.selectOne(appletStoreProductQueryWrapper);
            if (appletStoreServer==null){
                QueryWrapper<AppletVehiclesServer> appletVehiclesServerQueryWrapper = new QueryWrapper<>();
                appletVehiclesServerQueryWrapper.eq("server_id",findServerDetailVo.getProductId());
                appletVehiclesServerQueryWrapper.eq("group_buy_price",findServerDetailVo.getPrice());
                appletVehiclesServerQueryWrapper.orderByDesc("store_price");
                AppletVehiclesServer appletVehiclesServer = appletVehiclesServerMapper.selectOne(appletVehiclesServerQueryWrapper);
                productDetailsVo.setStorePrice(appletVehiclesServer.getStorePrice());
            }else {
                productDetailsVo.setStorePrice(appletStoreServer.getStorePrice());
            }*/

        }

//        if(findServerDetailVo.getProductType().equals("产品分类")) findServerDetailVo.setProductType("1");

        //评论信息
        List<CommentDto> commentList = appletCommentMapper.StoreProjectXianqing(findServerDetailVo.getProductId(), findServerDetailVo.getProductType());
        productDetailsVo.setCommentlist(commentList);
        productDetailsVo.setScore(appletCommentMapper.productComment(findServerDetailVo.getProductId()));

        return R.success(productDetailsVo);
    }

    @Override
    @Transactional
    public R insertPrivateProductOrServer(InsertPrivateProductOrServerVo in) {
        Long mid = getMid();
        Date date = new Date();

        Member member = memberMapper.selectById(mid);
        Store store = ceresStoreMapper.selectByMid(mid);
        PcVehicles pcVehicles = pcVehiclesMapper.selectByUserId(mid);

        //添加店铺独有的服务或产品
        int i = 0;
        if (in.getType() == 1) {
            //产品
            AppletProduct product = new AppletProduct();
            product.setProductName(in.getTitle());
            product.setShortName(in.getSubtitle());
            product.setRemarks(in.getRemarks());
            product.setProductImg(String.join(",", in.getImgList()));
            product.setContent(in.getContent());
            product.setBackgroundImg(String.join(",", in.getBackgroundImg()));
            product.setCategoryId(in.getServerCategoryId());
            product.setStorePrice(in.getStorePrice());
            product.setGroupBuyPrice(in.getGroupBuyPrice());
            product.setBelong(1);
            product.setCreateUser(String.valueOf(mid));
            product.setCreateTime(date);

            appletProductMapper.insert(product);

            if (member.getRoleType() == 0) {
                //商家
                AppletStoreProduct storeProduct = new AppletStoreProduct();
                storeProduct.setProductId(product.getId());//获取主键回填的id
                storeProduct.setStoreId(store.getId());
                storeProduct.setStorePrice(in.getStorePrice());
                storeProduct.setGroupBuyPrice(in.getGroupBuyPrice());
                storeProduct.setCreateUser(String.valueOf(mid));
                storeProduct.setCreateTime(date);
                i = appletStoreProductMapper.insert(storeProduct);

            } else {
                //理疗车
                AppletVehiclesProduct vehiclesProduct = new AppletVehiclesProduct();
                vehiclesProduct.setProductId(product.getId());//获取主键回填的id
                vehiclesProduct.setVehiclesId(pcVehicles.getId());
                vehiclesProduct.setStorePrice(in.getStorePrice());
                vehiclesProduct.setGroupBuyPrice(in.getGroupBuyPrice());
                vehiclesProduct.setCreateUser(String.valueOf(mid));
                vehiclesProduct.setCreateTime(date);
                i = appletVehiclesProductMapper.insert(vehiclesProduct);
            }
        } else {
            //服务
            AppletServer server = new AppletServer();
            server.setTitle(in.getTitle());
            server.setSubtitle(in.getSubtitle());
            server.setRemarks(in.getRemarks());
            server.setImg(String.join(",", in.getImgList()));
            server.setContent(in.getContent());
            server.setBackgroundImg(String.join(",", in.getBackgroundImg()));
            server.setServerCategoryId(in.getServerCategoryId());
            server.setStorePrice(in.getStorePrice());
            server.setGroupBuyPrice(in.getGroupBuyPrice());
            server.setBelong(1);
            server.setCreateUser(String.valueOf(mid));
            server.setCreateTime(date);

            appletServerMapper.insert(server);
            if (member.getRoleType() == 0) {
                //商家
                AppletStoreServer storeServer = new AppletStoreServer();
                storeServer.setServerId(server.getId());//获取主键回填的id
                storeServer.setStoreId(store.getId());
                storeServer.setStorePrice(in.getStorePrice());
                storeServer.setGroupBuyPrice(in.getGroupBuyPrice());
                storeServer.setCreateUser(String.valueOf(mid));
                storeServer.setCreateTime(date);
                i = appletStoreServerMapper.insert(storeServer);

            } else {
                //理疗车
                AppletVehiclesServer vehiclesServer = new AppletVehiclesServer();
                vehiclesServer.setServerId(server.getId());//获取主键回填的id
                vehiclesServer.setVehiclesId(pcVehicles.getId());
                vehiclesServer.setStorePrice(in.getStorePrice());
                vehiclesServer.setGroupBuyPrice(in.getGroupBuyPrice());
                vehiclesServer.setCreateUser(String.valueOf(mid));
                vehiclesServer.setCreateTime(date);
                i = appletVehiclesServerMapper.insert(vehiclesServer);
            }
        }
        return i > 0 ? R.success() : R.fail("添加失败");
    }

    @Override
    public R findCateGoryByType(Integer type) {
        List<Map<String, Object>> list = new ArrayList<>();
        if (type == 0) {
            //服务类别
            List<PcServerCategory> selectList = pcServerCategoryMapper.selectList(new QueryWrapper<>());
            for (PcServerCategory category : selectList) {
                Map<String, Object> map = new HashMap<>();
                map.put("value", category.getId());
                map.put("text", category.getServerName());
                list.add(map);
            }
        } else {
            List<CeresProductCategory> selectList = ceresProductCategoryMapper.selectList(new QueryWrapper<>());
            for (CeresProductCategory category : selectList) {
                Map<String, Object> map = new HashMap<>();
                map.put("value", category.getId());
                map.put("text", category.getCategoryName());
                list.add(map);
            }
        }
        return R.success(list);
    }

    @Override
    public R findServerProductByType(Integer type) {
        List<Map<String, Object>> list = new ArrayList<>();
        if (type != 0) {
            List<AppletProduct> products = appletProductMapper.selectList(new QueryWrapper<>());
            for (AppletProduct product : products) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", product.getId());
                map.put("serverName", product.getProductName());
                map.put("img", product.getProductImg());
                map.put("price", product.getGroupBuyPrice());
                list.add(map);
            }
        } else {
            List<AppletServer> servers = appletServerMapper.selectList(new QueryWrapper<>());
            for (AppletServer category : servers) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", category.getId());
                map.put("serverName", category.getTitle());
                map.put("img", category.getImg());
                map.put("price", category.getGroupBuyPrice());
                list.add(map);
            }
        }
        return R.success(list);
    }

    @Override
    public R findServerProductByName(String name) {
        List<Map<String, Object>> list = new ArrayList<>();

        List<AppletProduct> products = appletProductMapper.selectList(new QueryWrapper<AppletProduct>().like("product_name",name).eq("status","1").eq("is_delete","0"));
        for (AppletProduct product : products) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", product.getId());
            map.put("serverName", product.getProductName());
            map.put("img", product.getProductImg());
            map.put("price", product.getStorePrice());
            map.put("type","product");
            list.add(map);
        }
        List<AppletServer> servers = appletServerMapper.selectList(new QueryWrapper<AppletServer>().like("title",name).eq("shelves","0").eq("is_delete","0"));
        for (AppletServer category : servers) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", category.getId());
            map.put("serverName", category.getTitle());
            map.put("img", category.getImg());
            map.put("price", category.getStorePrice());
            map.put("type","server");
            list.add(map);
        }

        return R.success(list);
    }

    @Override
    public R insertPublicProductOrServer(InsertPublicOrServerVo in) {
        Long mid = getMid();
        Date date = new Date();

        Member member = memberMapper.selectById(mid);
        Store store = ceresStoreMapper.selectByMid(mid);
        PcVehicles pcVehicles = pcVehiclesMapper.selectByUserId(mid);

        AppletServer server = appletServerMapper.selectById(in.getId());
        AppletProduct product = appletProductMapper.selectById(in.getId());

        //添加公有的服务或产品
        int i = 0;

        if (in.getType() == 0) {
            //服务
            if (member.getRoleType() == 0) {
                //商家
                AppletStoreServer storeServer = new AppletStoreServer();
                storeServer.setServerId(server.getId());//获取主键回填的id
                storeServer.setStoreId(store.getId());
                storeServer.setStorePrice(server.getStorePrice());
                storeServer.setGroupBuyPrice(server.getGroupBuyPrice());
                storeServer.setCreateUser(String.valueOf(mid));
                storeServer.setCreateTime(date);
                i = appletStoreServerMapper.insert(storeServer);
            } else {
                //理疗车
                AppletVehiclesServer vehiclesServer = new AppletVehiclesServer();
                vehiclesServer.setServerId(server.getId());//获取主键回填的id
                vehiclesServer.setVehiclesId(pcVehicles.getId());
                vehiclesServer.setStorePrice(server.getStorePrice());
                vehiclesServer.setGroupBuyPrice(server.getGroupBuyPrice());
                vehiclesServer.setCreateUser(String.valueOf(mid));
                vehiclesServer.setCreateTime(date);
                i = appletVehiclesServerMapper.insert(vehiclesServer);
            }
        } else {
            //产品
            if (member.getRoleType() == 0) {
                //商家
                AppletStoreProduct storeProduct = new AppletStoreProduct();
                storeProduct.setProductId(product.getId());//获取主键回填的id
                storeProduct.setStoreId(store.getId());
                storeProduct.setStorePrice(product.getStorePrice());
                storeProduct.setGroupBuyPrice(product.getGroupBuyPrice());
                storeProduct.setCreateUser(String.valueOf(mid));
                storeProduct.setCreateTime(date);
                i = appletStoreProductMapper.insert(storeProduct);

            } else {
                //理疗车
                AppletVehiclesProduct vehiclesProduct = new AppletVehiclesProduct();
                vehiclesProduct.setProductId(product.getId());//获取主键回填的id
                vehiclesProduct.setVehiclesId(pcVehicles.getId());
                vehiclesProduct.setStorePrice(product.getStorePrice());
                vehiclesProduct.setGroupBuyPrice(product.getGroupBuyPrice());
                vehiclesProduct.setCreateUser(String.valueOf(mid));
                vehiclesProduct.setCreateTime(date);
                i = appletVehiclesProductMapper.insert(vehiclesProduct);
            }
        }
        return i > 0 ? R.success() : R.fail("添加失败");
    }

    @Override
    public R findMyServerProductByType(Integer type) {
        Long mid = getMid();
        List<MyServerOrProductVo> list = new ArrayList<>();
        Member member = memberMapper.selectById(mid);

        Store store = ceresStoreMapper.selectByMid(mid);
        PcVehicles pcVehicles = pcVehiclesMapper.selectByUserId(mid);
        if (member.getRoleType() == 0) {
            //商家
            if (type == 0) {
                //服务
                list = appletStoreServerMapper.selectByStoreId(store.getId());
            } else {
                //产品
                list = appletStoreProductMapper.selectByStoreId(store.getId());
            }
        } else {
            //理疗车
            if (type == 0) {
                //服务
                list = appletVehiclesServerMapper.selectByVehiclesId(pcVehicles.getId());
            } else {
                //产品
                list = appletVehiclesProductMapper.selectByVehiclesId(pcVehicles.getId());
            }
        }
        return R.success(list);
    }

    @Override
    public R updateMyServerProductByType(Integer type, Long id) {
        Long mid = getMid();
        Member member = memberMapper.selectById(mid);

        int i = 0;
        if (member.getRoleType() == 0) {
            //商家
            if (type == 0) {
                //服务
                i = appletStoreServerMapper.deleteById(id);
            } else {
                //产品
                i = appletStoreProductMapper.deleteById(id);
            }
        } else {
            //理疗车
            if (type == 0) {
                //服务
                i = appletVehiclesServerMapper.deleteById(id);
            } else {
                //产品
                i = appletVehiclesProductMapper.deleteById(id);
            }
        }
        //当要删除的服务/产品不是公有的时，删除在字典中的对应数据
/*        QueryWrapper<AppletServer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("belong",1);
//        appletServerMapper*/
        return i > 0 ? R.success() : R.fail("删除失败");
    }


}
